package com.spyu.utils;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * EasyExcel工具类
 *
 * @author spyu
 * @date 2023-03-08
 */
public final class EasyExcelUtils<T> {

    /**
     * 导出excel
     *
     * @param response
     * @param fileName
     * @param sheetName
     * @param clazz
     * @param dataList
     * @param <T>
     * @throws IOException
     */
    public static <T> void exportExcel(HttpServletResponse response,
                                       String fileName,
                                       String sheetName,
                                       Class<T> clazz,
                                       List<T> dataList) throws IOException {

        response.setCharacterEncoding("UTF-8");
        // 告知浏览器字节流下载
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition",
                "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));

        ExcelWriterBuilder excelWriterBuilder = new ExcelWriterBuilder();

        ExcelWriterSheetBuilder sheet = excelWriterBuilder.excelType(ExcelTypeEnum.XLSX)
                .file(response.getOutputStream())
                .autoCloseStream(true)
                .head(clazz)
                .sheet(sheetName);
        sheet.doWrite(dataList);
    }

    public static <T> List<T> importExcel(HttpServletRequest request,
                                          Class<T> clazz) throws IOException {
        return importExcel(request, clazz, 0, 1);
    }


    /**
     * 导入excel
     *
     * @param request
     * @param clazz
     * @param sheetno
     * @param headerRows
     * @param <T>
     * @return
     * @throws IOException
     */
    public static <T> List<T> importExcel(HttpServletRequest request,
                                          Class<T> clazz,
                                          int sheetno,
                                          int headerRows) throws IOException {
        List<T> resultList = new ArrayList<>();

        ExcelReaderBuilder builder = EasyExcelFactory.read().autoCloseStream(true).autoTrim(true)
                .registerReadListener(new AnalysisEventListener<T>() {

                    @Override
                    public void invoke(T t, AnalysisContext analysisContext) {
                        resultList.add(t);
                    }

                    @Override
                    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                        System.out.println("所有的数据都解析完毕");
                        resultList.clear();
                    }
                });

        builder.head(clazz).file(request.getInputStream())
                .ignoreEmptyRow(true)
                .headRowNumber(headerRows)
                .sheet(sheetno)
                .doRead();

        return resultList;
    }


}
